home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2006 April / DPPRO0406DVD.ISO / Essentials / Programming / Basic4GL / Setup Basic4GL v2.3.3.exe / $INSTDIR / Programs / AsteroidDemo.gb next >
Encoding:
Text File  |  2005-10-05  |  10.7 KB  |  330 lines

  1. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  2. ' Asteroid demo
  3.  
  4.     ' Constants
  5.     dim rangex#, rangey#
  6.     rangex# = 120
  7.     rangey# = 100
  8.     
  9.     ' Setup screen
  10.     glMatrixMode (GL_PROJECTION)
  11.     glLoadIdentity ()
  12.     glOrtho (0, rangex#, 0, rangey#, 0, 1)
  13.     glMatrixMode (GL_MODELVIEW)
  14.     TextMode (TEXT_OVERLAID)
  15.     
  16.     ' Player state
  17.     struc SPlayer
  18.         dim index
  19.         dim pos#(1), vel#(1)
  20.         dim dir#, lives, score, deadCounter, inGame
  21.         dim leftKey, rightKey, thrustKey, shootKey
  22.         dim wasShooting
  23.     endstruc
  24.     struc SAsteroid
  25.         dim pos#(1), size, vel#(1)
  26.         dim dir#, rot#
  27.     endstruc 
  28.     struc SBullet
  29.         dim pos#(1), vel#(1)
  30.         dim life
  31.     endstruc
  32.     
  33.     ' Initialise gamestate
  34.     const maxPlayers = 2
  35.     const maxAsteroids = 200
  36.     const maxBullets = 200
  37.     dim SPlayer player(maxPlayers), SPlayer ¤tPlayer, level
  38.     dim SAsteroid asteroids (maxAsteroids), asteroidCount, SAsteroid ¤tAsteroid
  39.     dim SBullet bullets (maxBullets), bulletCount, SBullet ¤tBullet
  40.     dim x, y, a$, i, j, speed, &pos#(), &dif#(), dev#
  41.     dim counter, alive
  42.     
  43.     ' Setup keys
  44.     player(1).index     = 1
  45.     player(1).leftKey   = VK_LEFT
  46.     player(1).rightKey  = VK_RIGHT
  47.     player(1).thrustKey = VK_UP
  48.     player(1).shootKey  = VK_SPACE
  49.     player(2).index     = 2
  50.     player(2).leftKey   = Asc("A")
  51.     player(2).rightKey  = Asc("D")
  52.     player(2).thrustKey = Asc("W")
  53.     player(2).shootKey  = VK_CONTROL
  54.     
  55.     ' Sound effects
  56.     dim shootSound, explodeSound
  57.     shootSound   = LoadSound ("sounds\laser.wav")
  58.     explodeSound = LoadSound ("sounds\gunshot5.wav")
  59.     
  60.     ' Put player 1 in game. 
  61.     goto start
  62.     
  63. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  64. ' Routines
  65.  
  66. Spawn:
  67.     currentplayer.deadCounter = 0
  68.     gosub CentrePlayer
  69.     return
  70.     
  71. CentrePlayer:
  72.     currentplayer.pos# = vec2 (rangex# / 2, rangey# / 2)
  73.     currentplayer.vel# = vec2 (0, 0)        
  74.     return
  75.     
  76. NewPlayer:
  77.     currentPlayer.inGame = true
  78.     currentPlayer.lives = 3
  79.     gosub Spawn
  80.     return    
  81.     
  82. NextLevel:    
  83.     ' Show next level bit
  84.     level = level + 1
  85.     
  86.     ' Create asteroids
  87.     asteroidCount = 3 + level * 2
  88.     for i = 1 to asteroidCount
  89.         asteroids (i).pos# = vec2 (rnd () % int (rangex#), rnd () % int (rangey#))
  90.         asteroids (i).dir# = rnd () % 360
  91.         asteroids (i).size = 3
  92.     next
  93.     
  94.     ' Centre players
  95.     for i = 1 to maxPlayers
  96.         ¤tPlayer = &player(i)
  97.         gosub CentrePlayer
  98.     next
  99.     
  100.     ' Remove bullets
  101.     bulletCount = 0
  102.     
  103.     counter = 100
  104.  
  105.     return
  106.     
  107. PrintText:
  108.     locate (TextCols () - Len(a$)) / 2, y: print a$
  109.     y = y + 1
  110.     return      
  111.     
  112. DrawPlayer:
  113.     if currentPlayer.inGame and currentPlayer.deadCounter = 0 then 
  114.         glPushMatrix ()
  115.         glTranslatef (currentPlayer.pos# (0), currentPlayer.pos# (1), 0)
  116.         glRotatef (currentPlayer.dir#, 0, 0, 1)
  117.         glScalef (1.5, 1.5, 1)
  118.         glColor3f (1, 1, 1)
  119.         glBegin (GL_LINE_LOOP)
  120.             glVertex2f (  0,  1)
  121.             glVertex2f (-.5, -1)
  122.             glVertex2f ( .5, -1)
  123.         glEnd ()
  124.         glPopMatrix ()
  125.     endif
  126.     return
  127.  
  128. MovePlayer:
  129.     if currentPlayer.inGame then
  130.         if currentPlayer.deadCounter > 0 then 
  131.             currentPlayer.deadCounter = currentPlayer.deadCounter - 1
  132.         else
  133.             if ScanKeyDown (currentPlayer.leftKey) then
  134.                 currentPlayer.dir# = currentPlayer.dir# + 5
  135.             endif
  136.             if ScanKeyDown (currentPlayer.rightKey) then
  137.                 currentPlayer.dir# = currentPlayer.dir# - 5
  138.             endif
  139.             if ScanKeyDown (currentPlayer.thrustKey) then
  140.                 currentPlayer.vel# = currentPlayer.vel# + MatrixRotateZ (currentPlayer.dir#) * vec2 (0, 0.0175)
  141.             endif
  142.             currentPlayer.pos# = currentPlayer.pos# + currentPlayer.vel#
  143.             &pos# = ¤tPlayer.pos#
  144.             gosub ClampPos
  145.             
  146.             if ScanKeyDown (currentPlayer.shootKey) and not currentPlayer.wasShooting and bulletCount < maxBullets then
  147.                 bulletCount = bulletCount + 1
  148.                 ¤tBullet = &bullets (bulletCount)
  149.                 currentBullet.pos# = currentPlayer.pos#
  150.                 currentBullet.vel# = currentPlayer.vel# + MatrixRotateZ (currentPlayer.dir#) * vec2 (0, 1)
  151.                 currentBullet.life = 50
  152.                 PlaySound (shootSound)
  153.             endif
  154.             currentPlayer.wasShooting = ScanKeyDown (currentPlayer.shootKey)
  155.         endif        
  156.     else
  157.         ' Player can join game by pressing shoot button
  158.         if ScanKeyDown (currentPlayer.shootKey) then
  159.             gosub NewPlayer
  160.         endif
  161.     endif
  162.     return
  163.     
  164. DrawAsteroid:
  165.     glPushMatrix ()
  166.     glTranslatef (currentAsteroid.pos# (0), currentAsteroid.pos# (1), 0)
  167.     glRotatef (currentAsteroid.rot#, 0, 0, 1)
  168.     glScalef (1.5 * currentAsteroid.size, 1.5 * currentAsteroid.size, 1)
  169.     glColor3f (1, 1, 1)
  170.     glBegin (GL_LINE_LOOP)
  171.         glVertex2f (-.51, -.88)
  172.         glVertex2f (-.07, -.95)
  173.         glVertex2f ( .07, -.72)
  174.         glVertex2f ( .53, -.72)
  175.         glVertex2f ( .88, -.06)
  176.         glVertex2f ( .94,  .53)
  177.         glVertex2f ( .49,  .52)
  178.         glVertex2f (-.03,  .90)
  179.         glVertex2f (-.45,  .69)
  180.         glVertex2f (-.92,  .20)
  181.         glVertex2f (-.51, -.49)
  182.     glEnd ()
  183.     glPopMatrix ()
  184.     return
  185.     
  186. MoveAsteroid:
  187.     speed = (4 - currentAsteroid.size)
  188.     currentAsteroid.rot# = currentAsteroid.rot# + speed * 1
  189.     currentAsteroid.pos# = currentAsteroid.pos# + MatrixRotateZ (currentAsteroid.dir#) * vec2 (0, 0.2) * speed
  190.     &pos# = ¤tAsteroid.pos#
  191.     gosub ClampPos
  192.     return
  193.  
  194. ClampPos:
  195.     if pos# (0) < 0 then 
  196.         pos# (0) = pos# (0) + rangex#
  197.     endif
  198.     if pos# (0) > rangex# then 
  199.         pos# (0) = pos# (0) - rangex#
  200.     endif
  201.     if pos# (1) < 0 then 
  202.         pos# (1) = pos# (1) + rangey#
  203.     endif
  204.     if pos# (1) > rangey# then 
  205.         pos# (1) = pos# (1) - rangey#
  206.     endif
  207.     return
  208.     
  209. DrawBullet:
  210.     glBegin (GL_POINTS)
  211.         glVertex2f (currentBullet.pos# (0), currentBullet.pos# (1))
  212.     glEnd ()
  213.     return    
  214.  
  215. end         ' Should never get here!
  216. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  217. ' Mainloop
  218. start:
  219.  
  220.     while true
  221.         gosub NextLevel
  222.         while asteroidCount > 0 or counter > 0
  223.             if asteroidCount = 0 then
  224.                 counter = counter - 1
  225.             endif
  226.         
  227.             ' Draw screen
  228.             glClear (GL_COLOR_BUFFER_BIT)
  229.             glDisable (GL_DEPTH_TEST)
  230.             glDisable (GL_CULL_FACE)
  231.             glMatrixMode (GL_MODELVIEW)
  232.             glLoadIdentity ()
  233.         
  234.             ' Players
  235.             for i = 1 to maxPlayers
  236.                 ¤tPlayer = &player(i)
  237.                 gosub DrawPlayer
  238.             next
  239.             
  240.             ' Asteroids 
  241.             for i = 1 to asteroidCount 
  242.                 ¤tAsteroid = &asteroids (i)
  243.                 gosub DrawAsteroid
  244.             next
  245.             
  246.             for i = 1 to bulletCount
  247.                 ¤tBullet = &bullets (i)
  248.                 gosub DrawBullet
  249.             next
  250.  
  251.             SwapBuffers ()
  252.             
  253.             ' Update gameplay (50 times per second)
  254.             while SyncTimer (20)
  255.  
  256.                 ' Move players
  257.                 Joy_Keys ()
  258.                 for i = 1 to maxPlayers
  259.                     ¤tPlayer = &player(i)
  260.                     gosub MovePlayer
  261.                 next            
  262.                 
  263.                 ' Move asteroids
  264.                 for i = 1 to asteroidCount
  265.                     ¤tAsteroid = &asteroids(i)
  266.                     gosub MoveAsteroid
  267.                 next
  268.  
  269.                 ' Move bullets
  270.                 i = 1
  271.                 while i <= bulletCount
  272.                     ¤tBullet = &bullets (i)
  273.                     currentBullet.pos# = currentBullet.pos# + currentBullet.vel#
  274.                     &pos# = ¤tBullet.pos#
  275.                     gosub ClampPos
  276.                     currentBullet.life = currentBullet.life - 1
  277.                     if currentBullet.life <= 0 then
  278.                         currentBullet = bullets (bulletCount)
  279.                         bulletCount = bulletCount - 1
  280.                     else
  281.                         i = i + 1
  282.                     endif
  283.                 wend
  284.                 
  285.                 ' Asteroid vs bullet collisions
  286.                 i = 1
  287.                 while i <= asteroidCount
  288.                     ¤tAsteroid = &asteroids (i)
  289.                     j = 1
  290.                     while j <= bulletCount
  291.                         ¤tBullet = &bullets (j)
  292.                     
  293.                         ' Calculate distance between bullet and asteroid
  294.                         if Length (currentBullet.pos# - currentAsteroid.pos#) < currentAsteroid.size * 1.5 then
  295.                             if currentAsteroid.size <= 1 then                                
  296.                             
  297.                                 ' Delete current asteroid
  298.                                 currentAsteroid         = asteroids (asteroidCount)
  299.                                 asteroidCount = asteroidCount - 1
  300.                             else
  301.                                 
  302.                                 ' Split current asteroid, by creating a duplicate
  303.                                 ' then shrinking both of them
  304.                                 currentAsteroid.size = currentAsteroid.size - 1
  305.                                 asteroidCount = asteroidCount + 1
  306.                                 asteroids (asteroidCount) = currentAsteroid
  307.                                 dev# = rnd () % 45 + 1
  308.                                 currentAsteroid.dir# = currentAsteroid.dir# + dev#
  309.                                 asteroids (asteroidCount).dir# = asteroids (asteroidCount).dir# - dev#
  310.                             endif
  311.  
  312.                             ' Delete bullet
  313.                             currentBullet = bullets (bulletCount)
  314.                             bulletCount = bulletCount - 1
  315.                             
  316.                             PlaySound (explodeSound)
  317.                             
  318.                             ' Exit loop
  319.                             j = bulletCount + 1
  320.                             i = i - 1           
  321.                         else 
  322.                             j = j + 1
  323.                         endif
  324.                     wend
  325.                     i = i + 1    
  326.                 wend
  327.             wend
  328.         wend
  329.     wend
  330.